﻿/// <reference name="MicrosoftAjax.js" /> 
/// <reference path="file://C:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/14/TEMPLATE/LAYOUTS/SP.core.debug.js" />
/// <reference path="file://C:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/14/TEMPLATE/LAYOUTS/SP.debug.js" />
/// <reference path="file://C:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/14/TEMPLATE/LAYOUTS/1031/CORE.debug.js"/> 
var ctx;
var camlQuery;
var fileRefArray;
var fullUrlForSingleDoc;
var listItem;
var notification;
var selectedFile;
var selectedListItemCollection;
var selectedItems;
var site;
var web;
var sdList;

// Prepared selected element for loading in client context und makes request 
function SendSingleDoc(ListId, ItemId) {
    var sdList;
    try {
        notification = SP.UI.Notify.addNotification('Outlook loading...', true);
        getEnvironmentForSendDocumentsFeature();
        sdList = web.get_lists().getById(ListId);
        listItem = sdList.getItemById(ItemId);
        ctx.load(listItem, "FileRef", "FileLeafRef");
        ctx.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededSingleDoc), Function.createDelegate(this, this.onQueryFailed));
    } catch (err) {
        alert("Error: " + err.get_Message());
        if (notification != null) { SP.UI.Notify.removeNotification(notification); }
    }
}

// Builts the documentUrl for selected document
function onQuerySucceededSingleDoc(sender, args) {
    fullUrlForSingleDoc = getRootUrl() + listItem.get_item('FileRef')
    getOutlook();
}

// Prepared selected elements for loading in client context und makes request
function sendMultipleDocuments() {
    var camlQuery;
    var queryObject;
    try {
        notification = SP.UI.Notify.addNotification('Outlook loading...', true);
        getEnvironmentForSendDocumentsFeature();
        sdList = web.get_lists().getById(SP.ListOperation.Selection.getSelectedList());
        ctx.load(sdList);
        ctx.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededSelectedList), Function.createDelegate(this, this.onQueryFailed));

    } catch (err) {
        alert("Error: " + err.get_message());
        if (notification != null) {
            SP.UI.Notify.removeNotification(notification);
        }
    }
}

// Gets all selected items
function onQuerySucceededSelectedList() {
    selectedItems = SP.ListOperation.Selection.getSelectedItems();
    itemsArray = new Array(selectedItems.length);
    for (i = 0; i < selectedItems.length; i++) {
        itemsArray[i] = sdList.getItemById(selectedItems[i].id);
    }

    for (i = 0; i < selectedItems.length; i++) {
        ctx.load(itemsArray[i], "FileRef", "FileLeafRef", "FSObjType");
    }
    ctx.executeQueryAsync(Function.createDelegate(this, this.succeededSelectedItems), Function.createDelegate(this, this.onQueryFailed));
}

// Builts an array of DocumentsUrls und calls outlook
function succeededSelectedItems() {

    fileRefArray = new Array(selectedItems.length);
    for (i = 0; i < itemsArray.length; i++) {
        
        fileRefArray[i] = getRootUrl() + itemsArray[i].get_item('FileRef');
    }
    deselectAllItems();
    getOutlook();
}

// Calls outlook and adds the attachments
function getOutlook() {
    var outlook;
    var otem;
    var modalDialog;
    try {
        modalDialog = SP.UI.ModalDialog.showWaitScreenWithNoClose("Send e-mail", "Please, wait...", 300, 400);
        outlook = new ActiveXObject('Outlook.Application');
        otem = outlook.CreateItem(0);
        if (fullUrlForSingleDoc != null) {
            otem.Attachments.Add(fullUrlForSingleDoc);
            if (selectedFile != null) {
                otem.Subject = selectedFile;
            }
            fullUrlForSingleDoc = null;
        } else {
            for (var i = 0; i < fileRefArray.length; i++) {
                otem.Attachments.Add(fileRefArray[i]);
            }
            fileRefArray = null;
            otem.Subject = "Documents from Share Point document library.";
        }
        otem.Display();
        otem.GetInspector.WindowState = 2;
    } catch (err) {
        alert("Error: " + err.message + "\n" +
        "The following may have cause this error: \n\n" +
        "1. The Outlook is not installed on the machine.\n" +
        "2. The “Initialize and Script ActiveX controls not marked as safe” option should be selected as “Enable” \n" +
            "Open Internet Explorer, go to Tools, Internet Options.\n" +
            "Click on the security page and make sure the \"Trusted sites\" zone is selected at the top.\n" +
            "Click on the Sites button.\n" +
            "Add current site to trusted sites and close the window.\n" +
            "Then click on Custom Level at the bottom.\n" +
            "In the following window you will see  settings for “Initialize and Script ActiveX controls not marked as safe”,\n" +
            "select the option “enable” under the same.\n" +
	 "3. This function was used not inside of share point domain.\n");
    } finally {
        otem = null;
        outlook = null;
        if (notification != null) SP.UI.Notify.removeNotification(notification);
        SP.UI.ModalDialog.commonModalDialogClose(1, '0');
        //SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);
    }
}

function onQueryFailed(sender, args) {
    alert('Failed ' + args.get_massage());
    if (notification != null) { SP.UI.Notify.removeNotification(notification); }
}

// Prepared variables for obtaining the basic environment(ClientContext, Web, Site)
function getEnvironmentForSendDocumentsFeature() {
    ctx = SP.ClientContext.get_current();
    web = ctx.get_web();
    site = ctx.get_site();
    ctx.load(site);
    ctx.load(web);
}

function getRootUrl() {
    var root;
    if (site.get_serverRelativeUrl() == "/") {
        root = site.get_url();
    }
    else {
        var index = site.get_url().indexOf(site.get_serverRelativeUrl());
        root = site.get_url().substr(0, index);
    }
    return root;
}